package com.dst.dao.impl;

import com.dst.dao.PhoneDao;
import com.dst.eneity.Brand;
import com.dst.eneity.Phone;
import com.dst.util.JdbcUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PhoneDaoImpl implements PhoneDao {

    @Override
    public List<Phone> selectAllPhoneDao() {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Phone> list=new ArrayList<>();
        try{
            conn = JdbcUtils.getConnection();
            String sql="select * from t_phone";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                BigDecimal price = rs.getBigDecimal("price");
                int state = rs.getInt("state");
                int brand_id = rs.getInt("brand_id");
                Phone phone = new Phone(id,name,price,state,brand_id);
                list.add(phone);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void deletByIdDao(int id) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        try{
            conn = JdbcUtils.getConnection();
            String sql="delete  from t_phone  where  id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            int i = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void addPhoneDao(Phone phone) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        try{
            conn = JdbcUtils.getConnection();
            String sql="insert into t_phone(name,state,price,brand_id) values (?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,phone.getName());
            pstmt.setInt(2,phone.getState());
            pstmt.setBigDecimal(3,phone.getPrice());
            pstmt.setInt(4,phone.getBrandid());
            int i = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Phone selectByIdPhoneDao(int id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Phone phone=null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select * from t_phone where id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int id1 = rs.getInt("id");
                String name = rs.getString("name");
                BigDecimal price = rs.getBigDecimal("price");
                int state = rs.getInt("state");
                int brand_id = rs.getInt("brand_id");
                phone = new Phone(id1,name,price,state,brand_id);
            }
        }catch (Exception e){
            e.printStackTrace();
        }  return phone;
    }

    @Override
    public void updatePhoneController(int id,Phone phone) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        try{
            conn = JdbcUtils.getConnection();
            String sql="update t_phone set name=?,price=?,state =?,brand_id=? where id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,phone.getName());
            pstmt.setBigDecimal(2,phone.getPrice());
            pstmt.setInt(3,phone.getState());
            pstmt.setInt(4,phone.getBrandid());
            pstmt.setInt(5,id);
            pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void batchDleteDao(int[] ids) {
        Connection conn=null;
        Statement st=null;
        try{
            conn = JdbcUtils.getConnection();
            st = conn.createStatement();
            String strid="";
            for (int id:ids
            ) {
                strid+=id+",";
            }
            strid+=0;
            String sql="delete from t_phone where id in ("+strid+")";
            st.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @Override
    public List<Phone> selectLimitPhoneDao(int begin, int end) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Phone> list=new ArrayList<>();
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select * from t_phone LIMIT ?,?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,begin);
            pstmt.setInt(2,end);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int id1 = rs.getInt("id");
                String name = rs.getString("name");
                BigDecimal price = rs.getBigDecimal("price");
                int state = rs.getInt("state");
                int brand_id = rs.getInt("brand_id");
                Phone phone = new Phone(id1,name,price,state,brand_id);
                list.add(phone);
            }
        }catch (Exception e){
            e.printStackTrace();
        }  return list;
    }



    @Override
    public List<Phone> select(int brandid) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Phone> list=new ArrayList<>();
        try{
            conn = JdbcUtils.getConnection();
            String sql="select * from t_phone where brand_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,brandid);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                BigDecimal price = rs.getBigDecimal("price");
                int state = rs.getInt("state");
                int brand_id = rs.getInt("brand_id");
                Phone phone = new Phone(id,name,price,state,brand_id);
                list.add(phone);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Phone> selectPage(int page, int pagesize) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Phone> list=new ArrayList<>();
        try{
            conn = JdbcUtils.getConnection();
            String sql="select * from t_phone limit ?,?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,page);
            pstmt.setInt(2,pagesize);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                BigDecimal price = rs.getBigDecimal("price");
                int state = rs.getInt("state");
                int brand_id = rs.getInt("brand_id");
                Phone phone = new Phone(id,name,price,state,brand_id);
                list.add(phone);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int selectCount() {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int num=0;
        try{
            conn = JdbcUtils.getConnection();
            String sql="select count(*) cou from t_phone";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
              num=rs.getInt("cou");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

}
